package com.umeox.ota.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.FloatRange;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.appcompat.view.menu.ListMenuItemView;
import androidx.appcompat.widget.ActionBarContainer;
import androidx.appcompat.widget.ActionBarContextView;
import androidx.appcompat.widget.ActionBarOverlayLayout;
import androidx.appcompat.widget.AlertDialogLayout;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.appcompat.widget.ButtonBarLayout;
import androidx.appcompat.widget.ContentFrameLayout;
import androidx.appcompat.widget.DialogTitle;
import androidx.appcompat.widget.FitWindowsLinearLayout;
import androidx.appcompat.widget.SwitchCompat;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.LayoutInflaterFactory;
import androidx.core.widget.NestedScrollView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.internal.NavigationMenuItemView;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.umeox.ota.R;
import com.umeox.ota.data.local.CustomConfig;
import com.zhy.android.percent.support.PercentLinearLayout;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.lang.reflect.Constructor;

/**
 * @author dashy
 * @date 2018/9/28.
 */
public class ViewUitls {
    // 暂时先使用 另一种方式设置:父布局为 RelativeLayout 的 textview全部都不设置颜色;

    public static int shiftColor(int color, @FloatRange(from = 0.0f, to = 2.0f) float by) {
        if (by == 1f) {
            return color;
        }
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);
        hsv[2] *= by;
        return Color.HSVToColor(hsv);
    }

    public static int darkenColor(int color) {
        return shiftColor(color, 0.9f);
    }

    public static int darkenColor(int color,float drakNum) {
        return shiftColor(color, drakNum);
    }

    public static int resolveResId(Context context, AttributeSet attrs, int attrId) {
        TypedArray ta = context.obtainStyledAttributes(attrs, new int[]{attrId});
        int result = ta.getResourceId(0, 0);
        ta.recycle();
        return result;
    }

    public static Drawable tintDrawable(Drawable drawable, ColorStateList colorStateList) {
        Drawable drawable1 = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable1, colorStateList);
        return drawable1;
    }

    public static LayoutInflaterFactory layoutInflaterFactory = new LayoutInflaterFactory() {
        String[] colors = CustomConfig.getInstance().theme.split(",");

        @SuppressLint("RestrictedApi")
        @Override
        public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
            final int viewId = resolveResId(context, attrs, android.R.attr.id);
//            Trace.d("ViewUitls", "ViewId: " + viewId + ", Name: " + name);
            View view = null;
            switch (name) {
                case "ImageView":
                case "android.support.v7.widget.AppCompatImageView":
                    ImageView imageView = new ImageView(context, attrs);
                    imageView.setBackgroundResource(R.color.transparent);
                    if (imageView.getId() == R.id.file_iv) {
                        //设置本地升级选择差分包的图标颜色
                        //imageView.setColorFilter(Color.WHITE);
                    } else {
                        imageView.setColorFilter(ThemeColor.textColorPrimary);
                    }
                    return imageView;

                case "ImageButton":
                case "android.support.v7.widget.AppCompatImageButton":
                    view = new ImageButton(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "android.support.v4.widget.DrawerLayout":
                    view = new DrawerLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "Toolbar":
                case "android.support.v7.widget.Toolbar":
                    Toolbar toolbar = new Toolbar(context, attrs);
                    toolbar.setBackgroundColor(ThemeColor.backgroundColor);
                    return toolbar;

                case "android.support.v7.widget.AppCompatTextView":
                case "TextView":
                    if ("RelativeLayout".equals(parent.getClass().getSimpleName())) {
                        return new TextView(context, attrs);
                    }
                    if (viewId == R.id.snackbar_text) {
                        return new Snackbar.SnackbarLayout(context, attrs);
                    }
                    TextView textView = new TextView(context, attrs);
                    textView.setTextColor(ThemeColor.textColorPrimary);
                    textView.setBackgroundResource(R.color.transparent);
                    if (parent instanceof LinearLayout && textView.getId() == android.R.id.message) {
                        // This is for a toast message, parents background should be transparent
                        parent.setBackgroundResource(R.color.transparent);
                    }
                    return textView;
                case "LinearLayout":
                    view = new LinearLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    if (view.getId() == R.id.ll_log_catch) {
                        view.setBackgroundColor(darkenColor(ThemeColor.backgroundColor));
                    }
                    break;

                case "RelativeLayout":
                    view = new RelativeLayout(context, attrs);
                    if (parent.getId() == -1) {
                        //菜单栏的layout暂不需要变色
                        break;
                    }
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "FrameLayout":
                    view = new FrameLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "android.support.v7.widget.FitWindowsLinearLayout":
                    view = new FitWindowsLinearLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "android.support.v7.widget.ContentFrameLayout":
                    view = new ContentFrameLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "com.zhy.android.percent.support.PercentRelativeLayout":
                    view = new PercentRelativeLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "android.support.design.widget.AppBarLayout":
                    view = new AppBarLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "com.zhy.android.percent.support.PercentLinearLayout":
                    view = new PercentLinearLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "android.support.v7.widget.AlertDialogLayout":
                    view = new AlertDialogLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;
                case "android.support.v7.widget.DialogTitle":
                    view = new DialogTitle(context, attrs);
                    ((DialogTitle) view).setTextColor(ThemeColor.textColorPrimary);
                    break;

                case "android.support.v7.view.menu.ListMenuItemView":
                    view = new ListMenuItemView(context, attrs);
                    break;

                case "android.support.v7.widget.ActionBarContextView":
                    view = new ActionBarContextView(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "android.support.v7.widget.ActionBarOverlayLayout":
                    view = new ActionBarOverlayLayout(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "android.support.v7.widget.ActionBarContainer":
                    view = new ActionBarContainer(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;

                case "Button":
                    view = new Button(context, attrs);
                    ((TextView) view).setTextColor(ThemeColor.backgroundColor);
                    view.setBackgroundColor(ThemeColor.textColorPrimary);
                    break;

                case "android.support.v7.widget.AppCompatCheckBox":
                case "CheckBox":
                    view = new CheckBox(context, attrs);
                    break;

                case "android.support.v7.widget.AppCompatRadioButton":
                case "RadioButton":
                    view = new RadioButton(context, attrs);
                    break;

                case "android.support.v7.widget.AppCompatEditText":
                case "EditText":
                    view = new EditText(context, attrs);
                    break;

                case "Switch":
                    view = new Switch(context, attrs);
                    break;

                case "android.support.v7.widget.SwitchCompat":
                    view = new SwitchCompat(context, attrs);
                    break;

                case "android.support.v7.widget.AppCompatSeekBar":
                case "SeekBar":
                    view = new SeekBar(context, attrs);
                    break;

                case "ProgressBar":
                case "me.zhanghai.android.materialprogressbar.MaterialProgressBar":
                    view = new ProgressBar(context, attrs);
                    break;

                case "android.support.v7.view.menu.ActionMenuItemView":
                    ActionMenuItemView actionMenuItemView = new ActionMenuItemView(context, attrs);
                    actionMenuItemView.setTextColor(ThemeColor.textColorPrimary);
                    return actionMenuItemView;

                case "android.support.v7.widget.RecyclerView":
                    RecyclerView recyclerView = new RecyclerView(context, attrs);
                    recyclerView.setBackgroundColor(ThemeColor.backgroundColor);
                    return recyclerView;

                case "android.support.v4.widget.NestedScrollView":
                    view = new NestedScrollView(context, attrs);
                    break;

                case "ListView":
                    view = new ListView(context, attrs);
                    break;

                case "ScrollView":
                    view = new ScrollView(context, attrs);
                    view.setBackgroundColor(ThemeColor.backgroundColor);
                    break;
                case "View":
                    view = new View(context, attrs);
                    view.setBackgroundColor(ThemeColor.textColorPrimary);
                    break;

                case "android.support.v4.view.ViewPager":
                    view = new ViewPager(context, attrs);
                    break;

                case "Spinner":
                case "android.support.v7.widget.AppCompatSpinner":
                    view = new AppCompatSpinner(context, attrs);
                    break;

                case "android.support.design.widget.TextInputLayout":
                    view = new TextInputLayout(context, attrs);
                    break;

                case "android.support.design.widget.TextInputEditText":
                    view = new TextInputEditText(context, attrs);
                    break;

                case "android.support.design.widget.TabLayout":
                    view = new TabLayout(context, attrs);
                    break;

                case "android.support.design.widget.NavigationView":
                    NavigationView navigationView = new NavigationView(context, attrs);
                    navigationView.setBackgroundColor(ThemeColor.textColorPrimary);
                    navigationView.setItemTextColor(ColorStateList.valueOf(ThemeColor.backgroundColor));
                    return navigationView;

                case "android.support.design.widget.BottomNavigationView":
                    view = new BottomNavigationView(context, attrs);
                    break;


                case "android.support.design.widget.FloatingActionButton":
                    view = new FloatingActionButton(context, attrs);
                    break;

                case "android.support.design.widget.CoordinatorLayout":
                    view = new CoordinatorLayout(context, attrs);
                    break;

                case "CheckedTextView":
                    view = new CheckedTextView(context, attrs);
                    ((CheckedTextView) view).setTextColor(ThemeColor.textColorPrimary);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        ((CheckedTextView) view).setCheckMarkTintList(ColorStateList.valueOf(ThemeColor.textColorPrimary));
                    }
                    break;

                case "com.android.internal.widget.ButtonBarLayout":
                    view = new ButtonBarLayout(context, attrs);
                    break;

                case "android.support.design.internal.NavigationMenuItemView":
                    view = new NavigationMenuItemView(context, attrs);
                    break;

                case "ViewStub":
                case "fragment":
                case "include":
                case "Space":
                    break;
                default:
                    try {
                        Class<?> aClass = Class.forName(name);
                        Class<?>[] interfaces = aClass.getInterfaces();
                        if (interfaces.length <= 0) {
                            break;
                        }
                        for (int i = 0; i < interfaces.length; i++) {
                            if (interfaces[i].getName().contains("IColor")) {
                                try {
                                    Constructor<?> constructor = aClass.getDeclaredConstructor(Context.class, AttributeSet.class);
                                    constructor.setAccessible(true);
                                    Object o = constructor.newInstance(context, attrs);
                                    view = (View) o;
                                    IColor iColor = (IColor) o;
                                    iColor.setColor(darkenColor(ThemeColor.textColorPrimary));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    break;
            }
            return view;
        }
    };
}